package na.learn.java.sorting;

import java.util.Comparator;

public class StringComparator implements Comparator {

	public int compare(Object arg0, Object arg1) {
		if ((arg0 instanceof Sortable) && (arg1 instanceof Sortable)) {
			String str1 = ((Sortable)arg0).getStringMessage();
			String str2 = ((Sortable)arg1).getStringMessage();
			
			char[] charArray1 = ((String) str1).toCharArray();
			char[] charArray2 = ((String) str2).toCharArray();
			int length1 = charArray1.length;
			int length2 = charArray2.length;
			int length = length1 < length2 ? length1 : length2;
			int count = -1;
			while (++count < length && charArray1[count] == charArray2[count])
				;

			int num1 = length1 > count ? charArray1[count] : 0;
			int num2 = length2 > count ? charArray2[count] : 0;

			return num1 - num2;

		}
		return 0;
	}

}
